package com.cmict.data.infra.controller.admin.rdbtabledraft;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.rdbtabledraft.vo.*;
import com.cmict.data.infra.dal.dataobject.rdbtabledraft.RdbTableDraftDO;
import com.cmict.data.infra.convert.rdbtabledraft.RdbTableDraftConvert;
import com.cmict.data.infra.service.rdbtabledraft.RdbTableDraftService;

@Api(tags = "管理后台 - 草稿")
@RestController
@RequestMapping("/infra/rdb-table-draft")
@Validated
public class RdbTableDraftController {

    @Resource
    private RdbTableDraftService rdbTableDraftService;

    @PostMapping("/create")
    @ApiOperation("创建草稿")
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:create')")
    public CommonResult<Long> createRdbTableDraft(@Valid @RequestBody RdbTableDraftCreateReqVO createReqVO) {
        return success(rdbTableDraftService.createRdbTableDraft(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新草稿")
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:update')")
    public CommonResult<Boolean> updateRdbTableDraft(@Valid @RequestBody RdbTableDraftUpdateReqVO updateReqVO) {
        rdbTableDraftService.updateRdbTableDraft(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除草稿")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:delete')")
    public CommonResult<Boolean> deleteRdbTableDraft(@RequestParam("id") Long id) {
        rdbTableDraftService.deleteRdbTableDraft(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得草稿")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:query')")
    public CommonResult<RdbTableDraftRespVO> getRdbTableDraft(@RequestParam("id") Long id) {
        RdbTableDraftDO rdbTableDraft = rdbTableDraftService.getRdbTableDraft(id);
        return success(RdbTableDraftConvert.INSTANCE.convert(rdbTableDraft));
    }

    @GetMapping("/list")
    @ApiOperation("获得草稿列表")
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:query')")
    public CommonResult<List<RdbTableDraftRespVO>> getRdbTableDraftList(@RequestParam("keyword") String keyword) {
        List<RdbTableDraftDO> list = rdbTableDraftService.getRdbTableDraftList(keyword);
        return success(RdbTableDraftConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得草稿分页")
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:query')")
    public CommonResult<PageResult<RdbTableDraftRespVO>> getRdbTableDraftPage(@Valid RdbTableDraftPageReqVO pageVO) {
        PageResult<RdbTableDraftDO> pageResult = rdbTableDraftService.getRdbTableDraftPage(pageVO);
        return success(RdbTableDraftConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出草稿 Excel")
    @PreAuthorize("@ss.hasPermission('sys:rdb-table-draft:export')")
    @OperateLog(type = EXPORT)
    public void exportRdbTableDraftExcel(@Valid RdbTableDraftExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RdbTableDraftDO> list = rdbTableDraftService.getRdbTableDraftList(exportReqVO);
        // 导出 Excel
        List<RdbTableDraftExcelVO> datas = RdbTableDraftConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "草稿.xls", "数据", RdbTableDraftExcelVO.class, datas);
    }

}
